const state = {
    pack: [],
    packCard: null,
    reward: [],
}
const getters = {
    getPack(state) {
        return state.pack
    },
    getPackCard(state) {
        return state.packCard
    },
    getReward(state) {
        return state.reward
    }
}
const mutations = {
    //初始化pack
    setPack(state, param) {
        state.pack = param
    },
    setPackCard(state, param) {
        state.packCard = param
    },
    // 更新pack
    insertPack(state, param) {
        let insert = true
        if (param.icon) {
            for (let i = 0, len = state.pack.length; i < len; i++) {
                if (state.pack[i].icon === param.icon) {
                    insert = false
                    return
                }
            }
        }
        if (insert) {
            param.packId = 0
            if (state.pack.length > 0) {
                param.packId = Math.max.apply(Math, state.pack.map(function (o) {
                    return o.packId + 1
                }))
            }
            state.pack.push(param)
        }
    },
    //删除背包中icon 
    deletePack(state, id) {
        for (let i = 0, len = state.pack.length; i < len; i++) {
            if (state.pack[i].packId === id) {
                state.pack.splice(i, 1)
                break
            }
        }
    },
    //   加入奖励池
    insertReward(state, param) {
        state.reward.push(param)
    },
    //    清空奖励池
    deleteReward(state) {
        if (state.reward.length > 0) {
            state.reward.splice(0, state.reward.length)
        }
    }
}
const actions = {
    setPack({ commit }, param) {
        commit("setPack", param)
    },
    setPackCard({ commit }, param) {
        commit("setPackCard", param)
    },
    deletePack({ commit }, id) {
        commit("deletePack", id)
    },
    insertPack({ commit }, param) {
        commit("insertPack", param)
    },
    insertReward({ commit }, param) {
        commit("insertReward", param)
    },
    deleteReward({ commit }) {
        commit("deleteReward")
    }
}
export default {
    namespaced: true, //用于在全局引用此文件里的方法时标识这一个的文件名
    state,
    getters,
    mutations,
    actions
}